using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.TextCore.Text;

/// <summary>
/// Idle̬状态
/// </summary>
public class IdleState : IFsmState
{
    private float m_CooldownTimer;

    public void Enter(Entity entity, params object[] args)
    {
        Logger.Log($"[IdleState] Enter===>>> {entity.name}");
        if (entity is Plant plant)
        {
            entity.Animator.SetBool("attack", false);
            
            // 如果是Jalapeno，等待一个很短的准备时间后立即触发攻击
            if (plant is Jalapeno)
            {
                // Jalapeno在Idle状态中等待0.1秒后触发攻击（给玩家一个准备阶段的视觉反馈）
                m_CooldownTimer = 0.1f;
                Logger.Log("IdleState", $"{entity.name} 是Jalapeno，将在0.1秒后触发技能");
            }
            else
            {
                m_CooldownTimer = entity.EntityData.AttackData.Interval;
            }
        }
        else if (entity is Zombie) {
            entity.FSMComponent.ChangeStatus(FSMType.Move);
        }
    }

    public void Exit(Entity entity, params object[] args)
    {

    }

    public void Process(Entity entity, params object[] args)
    {
        if (entity == null) return;

        // 检查是否是土豆地雷（被动触发，不应该自动攻击）
        if (entity is PotatoMine)
        {
            // 土豆地雷是被动触发的，不应该在Idle状态中自动切换到攻击状态
            // 它只会在僵尸踩到时通过 OnTriggerEnter2D 触发
            return;
        }

        if (m_CooldownTimer > 0)
            m_CooldownTimer -= Time.deltaTime;

        if (m_CooldownTimer > 0) return;
        
        // 触发攻击
        Logger.Log("IdleState", $"{entity.name} Cooldown结束，切换到攻击状态");
        entity.FSMComponent.ChangeStatus(FSMType.Attack);
    }
}
